// 引入公共组件
require('../less/commCss/basic.css')
// 引入文字样式
require('../fonts/iconfont.css')
// 引入当前css
require('../less/myhome.less')
// 引入weui
require('../lib/weui/weui.min.css')
const weui = require('../lib/weui/weui.js')
// 引入http模块,ajax请求
const {
    $http
} = require('../lib/commonJs/http.js')
const utils = require('../lib/commonJs/utils.js')

// 是否登录
utils.isLogin()

/**
 * 获取元素
 */
const username = document.querySelector('.username')
const userimg = document.querySelector('.overimg img')
const usertitle = document.querySelector('.usertext')
const moveTimer = document.querySelector('.move-timer')
const moveConsume = document.querySelector('.move-consume')
const outlogin = document.querySelector('.outlogin')


/**
 * 添加导航栏
 */
utils.showNav('myhome')

/**
 * 获取用户id
 */

const userid = localStorage.getItem('userId')

/**
 * 渲染用户信息
 */
function getuserinfo() {
    $http.get(`/users/mysportsBadge?userId=${userid}`, function (res) {
        const userinfo = res.data.user;
        const sportsinfo = res.data.sports;
        localStorage.setItem('userinfo', JSON.stringify(userinfo))
        localStorage.setItem('sportsinfo', JSON.stringify(sportsinfo))
        username.textContent = userinfo.nickname;
        if (userinfo.imgurl) userimg.src = userinfo.imgurl;
        if (userinfo.sign) usertitle.textContent = userinfo.sign;
        moveTimer.textContent = sportsinfo.times;
        moveConsume.textContent = sportsinfo.calorie
    })
}
getuserinfo()

outlogin.addEventListener('click', function () {
    weui.confirm('是否确定退出登录', function () {
        localStorage.clear()
        utils.isLogin()
    })
})